package com.linlang.grasharepro.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.linlang.grasharepro.R;
import com.linlang.grasharepro.activity.fragment.MainPage;
import com.linlang.grasharepro.activity.fragment.MessagePage;
import com.linlang.grasharepro.activity.fragment.MyCenterPage;
import com.linlang.grasharepro.activity.fragment.ReleasePage;
import com.linlang.grasharepro.activity.fragment.SearchPage;
import com.linlang.grasharepro.adapter.FragAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends FragmentActivity {

    private ViewPager viewPager;//对应的viewPager
    private List<Fragment> mDatas;
    private LinearLayout mainfram,releaseframe,searchfram,messagefram,mycenterfram;
    private TextView mainframtext,releaseframetext,messageframtext,mycenterframtext,searchtext;
    private ImageView mainframimage,releaseframeimage,searchframimage,messageframimage,mycenterframimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        viewPager.setCurrentItem(2);
    }

    private void initView() {
        viewPager = (ViewPager) this.findViewById(R.id.viewPager);
        mainfram = (LinearLayout)this.findViewById(R.id.fram_main);
        releaseframe = (LinearLayout)this.findViewById(R.id.fram_release);
        searchfram = (LinearLayout)this.findViewById(R.id.fram_search);
        messagefram = (LinearLayout)this.findViewById(R.id.fram_message);
        mycenterfram = (LinearLayout)this.findViewById(R.id.fram_mine);
        mainframtext = (TextView)this.findViewById(R.id.fram_main_text);
        releaseframetext = (TextView)this.findViewById(R.id.fram_release_text);
        messageframtext = (TextView)this.findViewById(R.id.fram_message_text);
        mycenterframtext = (TextView)this.findViewById(R.id.fram_mine_text);
        searchtext = (TextView)this.findViewById(R.id.fram_search_text);
        mainframimage = (ImageView)this.findViewById(R.id.fram_main_image);
        releaseframeimage = (ImageView)this.findViewById(R.id.fram_release_image);
        searchframimage = (ImageView)this.findViewById(R.id.fram_search_image);
        messageframimage = (ImageView)this.findViewById(R.id.fram_message_image);
        mycenterframimage = (ImageView)this.findViewById(R.id.fram_mine_image);
        mainfram.setOnClickListener(new MyOnClickListener(0));
        releaseframe.setOnClickListener(new MyOnClickListener(1));
        searchfram.setOnClickListener(new MyOnClickListener(2));
        messagefram.setOnClickListener(new MyOnClickListener(3));
        mycenterfram.setOnClickListener(new MyOnClickListener(4));
        MainPage mainPage = new MainPage();
        MessagePage messagePage = new MessagePage();
        MyCenterPage myCenterPage = new MyCenterPage();
        ReleasePage releasePage = new ReleasePage();
        SearchPage searchPage = new SearchPage();
        mDatas = new ArrayList<Fragment>();
        mDatas.add(releasePage);
        mDatas.add(searchPage);
        mDatas.add(mainPage);
        mDatas.add(messagePage);
        mDatas.add(myCenterPage);
        FragAdapter fragAdapter = new FragAdapter(getSupportFragmentManager(),mDatas);
        viewPager.setAdapter(fragAdapter);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                resetBottom();
                switch (position) {
                    case 0:
                        mainframimage.setImageResource(R.drawable.main_index_tuan_pressed);
                        mainframtext.setTextColor(Color.parseColor("#f1938b"));
                        break;
                    case 1:
                        releaseframeimage.setImageResource(R.drawable.main_index_search_pressed);
                        releaseframetext.setTextColor(Color.parseColor("#f1938b"));
                        break;
                    case 2:
                        searchframimage.setImageResource(R.drawable.main_index_checkin_pressed);
                        searchtext.setTextColor(Color.parseColor("#f1938b"));
                        break;
                    case 3:
                        messageframimage.setImageResource(R.drawable.main_index_more_pressed);
                        messageframtext.setTextColor(Color.parseColor("#f1938b"));
                        break;
                    case 4:
                        mycenterframimage.setImageResource(R.drawable.main_index_my_pressed);
                        mycenterframtext.setTextColor(Color.parseColor("#f1938b"));
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 2;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            viewPager.setCurrentItem(index);
        }

    }

    private void resetBottom(){
        mainframimage.setImageResource(R.drawable.search_bottem_tuan);
        releaseframeimage.setImageResource(R.drawable.search_bottem_search);
        searchframimage.setImageResource(R.drawable.search_bottem_checkin);
        messageframimage.setImageResource(R.drawable.search_bottem_more);
        mycenterframimage.setImageResource(R.drawable.search_bottem_my);
        mainframtext.setTextColor(getResources().getColor(
                R.color.search_bottem_textcolor));
        releaseframetext.setTextColor(getResources().getColor(
                R.color.search_bottem_textcolor));
        messageframtext.setTextColor(getResources().getColor(
                R.color.search_bottem_textcolor));
        mycenterframtext.setTextColor(getResources().getColor(
                R.color.search_bottem_textcolor));
        searchtext.setTextColor(getResources().getColor(
                R.color.search_bottem_textcolor));
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode == KeyEvent.KEYCODE_BACK)
        {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;

    private void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务

        } else {
            finish();
            System.exit(0);
        }
    }
}
